<script setup>
import roleApi from "@/api/role.js";
import {ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import { Plus } from '@element-plus/icons-vue'
import router from "@/router/index.js";
import permissionApi from "@/api/permission.js";
//数据表格
const list = ref([])
const total = ref(0)
//分页信息和搜索条件
const roleQuery = ref({
  name: '',
  page: 1,
  limit: 10
})
const dateTimeRange = ref([])

/*roleApi.list(roleQuery.value).then(result => {
    list.value = result.data.records
    total.value = result.data.total
})*/

const loadData = () => {
  roleApi.list(roleQuery.value).then(result => {
    if (result.code == 0) {
      list.value = result.data.records
      total.value = result.data.total
    }
  })
}

loadData()

const onSearch = () => {
  roleQuery.value.page = 1
  if(dateTimeRange.value && dateTimeRange.value.length == 2) {
    roleQuery.value.beginUpdateTime = dateTimeRange.value[0]
    roleQuery.value.endUpdateTime = dateTimeRange.value[1]
  }
  loadData()
}

const reset = () => {
  roleQuery.value.name = ''
  dateTimeRange.value = []
}

//根据id删除
const deleteById = (id) => {
  ElMessageBox.confirm(
      '您确认要删除么?',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    roleApi.deleteById(id).then(result => {
      if (result.code == 0) {
        ElMessage.success(result.msg)
        loadData()
      } else {
        ElMessage.error(result.msg)
      }
    })
  })
}

//批量删除
const ids = ref([])
const handleSelectionChange = (rows) => {
  console.log('多选', rows)
  ids.value = rows.map(function (row) {
    return row.id;
  })
  console.log(ids.value)
}

const deleteAll = () => {
  ElMessageBox.confirm(
      '您确认要删除么?',
      '警告',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    roleApi.deleteAll(ids.value).then(result => {
      if (result.code == 0) {
        ElMessage.success(result.msg)
        loadData()
      } else {
        ElMessage.error(result.msg)
      }
    })
  })
}

//添加、编辑
const dialogFormVisible = ref(false)
const role = ref({})
const showAddDialog = () => {
  role.value = {}
  dialogFormVisible.value = true
}

const showUpdateDialog = (id) => {
  dialogFormVisible.value = true
  role.value = {}
  roleApi.selectById(id).then(result => {
    if (result.code == 0) {
      role.value = result.data
    }
  })
}

const addOrUpdate = () => {
  if (role.value.id) {
    roleApi.update(role.value).then(result => {
      if (result.code == 0) {
        ElMessage.success(result.msg)
        dialogFormVisible.value = false
        loadData()
      } else {
        ElMessage.error(result.msg)
      }
    })
  } else {
    roleApi.add(role.value).then(result => {
      if (result.code == 0) {
        ElMessage.success(result.msg)
        dialogFormVisible.value = false
        loadData()
      } else {
        ElMessage.error(result.msg)
      }
    })
  }
}

//权限分配的树形结构
const dialogPermissionVisible = ref(false)
const treeData = ref([])
const treeRef = ref()
const defaultProps = ref({
  children: 'children',
  label: 'name'
})
const showAssignedPermissionDialog = (row) => {
  role.value = row;
  dialogPermissionVisible.value = true;
  treeData.value = [];
  roleApi.selectAssignedPermission(row.id).then((result) => {
    treeData.value = result.data.permissionVOList;
    let checkedLeafIdList = [];
    getCheckedLeafIdList(result.data.permissionVOList, result.data.assignedPermissionIdList, checkedLeafIdList);
    treeRef.value.setCheckedKeys(checkedLeafIdList);
  });
}

const getCheckedLeafIdList = (permissionVOList, assignedPermissionIdList, checkedLeafIdList) => {
  permissionVOList.forEach(permissionVO => {
    assignedPermissionIdList.forEach(id => {
      //这个角色下面的权限，而且是没有孩子的叶子节点
      if (permissionVO.id == id && permissionVO.children.length == 0) {
        checkedLeafIdList.push(id)
      } else if(permissionVO.id == id && permissionVO.children.length != 0) {
        getCheckedLeafIdList(permissionVO.children, assignedPermissionIdList, checkedLeafIdList)
      }
    })
  })
}

const assignPermission = () => {
  let checkedNodes = treeRef.value.getCheckedNodes(false, true);
  console.log(checkedNodes);
  let permissionIds = checkedNodes.map((node) => node.id);
  permissionIds = permissionIds.join(',');
  roleApi.assignPermission(role.value.id, permissionIds).then((result) => {
    ElMessage({message: result.msg, type: 'success',})
    dialogPermissionVisible.value = false;
  });
}

</script>

<template>
  <el-card class="">
    <template #header>
      <div class="header">
        <el-button type="primary" @click="showAddDialog">添加</el-button>
        <el-button type="primary" @click="deleteAll">批量删除</el-button>
      </div>
    </template>
    <el-form :inline="true">
      <el-form-item label="名字">
        <el-input v-model="roleQuery.name" placeholder="请输入名字" clearable  style="width: 200px"/>
      </el-form-item>
      <el-form-item label="更新时间" :label-width="100">
        <el-date-picker
            clearable
            v-model="dateTimeRange"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button type="warning" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%" ref="multipleTableRef" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column fixed prop="id" label="ID"/>
      <el-table-column prop="name" label="名字"/>
      <el-table-column prop="code" label="编码"/>
      <el-table-column prop="description" label="简介"/>
      <el-table-column prop="updateTime" label="更新时间" width="180px"/>
      <el-table-column align="center" width="200px" fixed="right" label="操作">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="showUpdateDialog(row.id)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteById(row.id)">删除</el-button>
          <el-button size="small" type="success" @click="showAssignedPermissionDialog(row)">权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:current-page="roleQuery.page"
        v-model:page-size="roleQuery.limit"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @change="loadData"
        style="margin-top: 20px; justify-content: flex-end"
    />
  </el-card>

  <!--添加、编辑弹出框-->
  <el-dialog v-model="dialogFormVisible" :title="title" width="60%" :lock-scroll="false">
    <el-form :model="role">
      <el-form-item label="名字" :label-width="60">
        <el-input v-model="role.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" :label-width="60">
        <el-input v-model="role.code" autocomplete="off" />
      </el-form-item>
      <el-form-item label="简介" :label-width="60">
        <el-input v-model="role.description" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addOrUpdate">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!--    权限分配的dialog-->
  <el-dialog
      title="分配权限"
      v-model="dialogPermissionVisible"
      width="40%" :lock-scroll="false">
    <el-tree
        :data="treeData"
        ref="treeRef"
        show-checkbox
        node-key="id"
        default-expand-all
        :props="defaultProps">
    </el-tree>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="assignPermission()">保存</el-button>
        <el-button  @click="dialogPermissionVisible = false">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
</style>

